package com.zx.web.servelt;

import com.zx.bean.Goods;
import com.zx.web.service.AdminGoodsService;
import com.zx.web.service.impl.AdminGoodsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.servelt
 * @Description:
 * @Date 2022/9/16 15:37
 */
@WebServlet("/admingoodsservlet")
public class AdminGoodsServlet extends BaseServlet{

    AdminGoodsService adminGoodsService = new AdminGoodsServiceImpl();

    /**
     * 展示商品
     * @param req
     * @param resp
     * @return
     */
    public String getGoodsList(HttpServletRequest req, HttpServletResponse resp){
        List<Goods> goodsList = adminGoodsService.getGoodsList();
        req.setAttribute("goodsList",goodsList);
        return "forward:/admin/showGoods.jsp";
    }


    /**
     * 添加商品
     * @param req
     * @param resp
     * @return
     */
    public String addGoods(HttpServletRequest req, HttpServletResponse resp){

        String name = req.getParameter("name");
        int typeid = Integer.parseInt(req.getParameter("typeid"));
        String pubdate = req.getParameter("pubdate");
        int price = Integer.parseInt(req.getParameter("price"));
        int star = Integer.parseInt(req.getParameter("star"));
        String picture = req.getParameter("picture");
        String intro = req.getParameter("intro");

        Date date =null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(pubdate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Goods goods = new Goods(name, new java.sql.Date(date.getTime()),picture,price,star,intro,typeid);

        adminGoodsService.addGoods(goods);

        return "redirect:/admingoodsservlet?method=getGoodsList";
    }
}
